package com.jp.springmvc.controller;

import com.jp.springmvc.vo.Spittle;
import com.jp.springmvc.repository.SpittleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Title: SpittleController
 * Description:
 *
 * @author zifan_jp
 * @version 1.0
 * @since Date: 2019-11-21 15:33
 * Copyright: Copyright (c) zifan_jp
 * ModificationHistory   Who    When    What
 * -----------------------------------------------
 */
@Controller
@RequestMapping("/spittles")
public class SpittleController {
    private final SpittleRepository spittleRepository;

    @Autowired
    public SpittleController(SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Spittle> spittles(@RequestParam(value="max",defaultValue = "1000000000") long max, int count){
        return spittleRepository.findSpittles(max,count);
    }
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public String showSpittle(@RequestParam("spittle_id") long spittleId,Model model){
        model.addAttribute(spittleRepository.findOne(spittleId));
        return "spittle";
    }
    @RequestMapping(value = "/{spittleId}",method = RequestMethod.GET)
    public String spittle(@PathVariable long spittleId, Model model){
        model.addAttribute(spittleRepository.findOne(spittleId));
        return "spittle";
    }
}
